public member function
<bitset>

std::bitset::operator[]

     bool operator[] (size_t pos) const;
reference operator[] (size_t pos);
Access bit
The function returns the value (or a reference) to the bit at position pos.

With this operator, no range check is performed. Use bitset::test to access the value with bitset bounds checked.

Parameters

pos
Order position of the bit whose value is accessed.
Order positions are counted from the rightmost bit, which is order position 0.
size_t is an unsigned integral type.

Return value

The bit at position pos.

If the bitset object is const-qualified, the function returns a bool value. Otherwise, it returns a value of the special member type reference, which emulates a bool value with reference-semantics with respect to one bit in the bitset (see bitset::reference).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// bitset::operator[]
#include <iostream>       // std::cout
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> foo;

  foo[1]=1;             // 0010
  foo[2]=foo[1];        // 0110

  std::cout << "foo: " << foo << '\n';

  return 0;
}


Output:

foo: 0110

Data races

The bitset is accessed (neither the const nor the non-const versions modify the container).
The reference returned by the non-const version can be used to access or modify the bits in the bitset. Notice that modifying a single bit may have effects on an undetermined number of other bits in the bitset, thus rendering concurrent access/modification of different bits not thread-safe.

Exception safety

If pos is not a valid bit position, it causes undefined behavior.
Otherwise, if an exception is thrown by this member function, the bitset is left in a valid state (basic guarantee).

See also